using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace buildingLib.daily
{
    [Serializable()]
    public class DocLogs :
            BusinessListBase<DocLogs, DocLog>
    {
        #region Authorization Rules

        public static bool CanAddObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanGetObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanEditObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        public static bool CanDeleteObject()
        {
            // TODO: customize to check user role
            //return ApplicationContext.User.IsInRole("");
            return true;
        }

        #endregion

        #region Factory Methods

        public static DocLogs NewDocLogs()
        {
            return new DocLogs();
        }

        public static DocLogs GetDocLogs()
        {
            return DataPortal.Fetch<DocLogs>(new Criteria());
        }

        private DocLogs()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {       
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            RaiseListChangedEvents = false;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_GGetDocLogsAll");
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                while (dr.Read())
                {
                    this.Add(DocLog.GetDocLog(dr));
                }
            }
            RaiseListChangedEvents = true;
        }

        protected override void DataPortal_Update()
        {
            RaiseListChangedEvents = false;
            foreach (DocLog item in DeletedList)
                item.DeleteSelf();
            DeletedList.Clear();

            foreach (DocLog item in this)
                if (item.IsNew)
                    item.Insert();
                else
                    item.Update();
            RaiseListChangedEvents = true;
        }

        #endregion
    }
}


